#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@File ：OrderModel.py
@Author ：zqy
@Email : zqingy@work@163.com
@note:
"""
import enum

from sqlalchemy import Column, Integer, String, Numeric, Boolean, ForeignKey, DateTime, Enum, Text
from sqlalchemy.orm import relationship

from application.util.MysqlUtil import mysql_db


class OrderStatus(enum.Enum):
    PENDING = '待支付'
    SHIPPED = '配送中'
    COMPLETED = '已完成'
    CANCELLED = '已取消'


class Order(mysql_db.Model):
    __tablename__ = 'order'
    __table_args__ = {'comment': '订单表'}

    id = Column(Integer, primary_key=True, comment="订单ID")
    user_id = Column(Integer, ForeignKey('user.id'), comment="下单用户ID")
    total_price = Column(Numeric(10, 2), comment="订单总金额")
    status = Column(Enum(OrderStatus), default=OrderStatus.PENDING, comment="订单状态")

    user = relationship("User", back_populates="orders")
    items = relationship("OrderItem", back_populates="order")


class OrderItem(mysql_db.Model):
    __tablename__ = 'order_item'
    __table_args__ = {'comment': '订单商品明细表'}

    id = Column(Integer, primary_key=True, comment="订单项ID")
    order_id = Column(Integer, ForeignKey('order.id'), comment="订单ID")
    product_id = Column(Integer, ForeignKey('product.id'), comment="商品ID")
    quantity = Column(Integer, default=1, comment="购买数量")
    price = Column(Numeric(10, 2), comment="购买时单价")

    order = relationship("Order", back_populates="items")
    product = relationship("Product", back_populates="order_items")


class CartItem(mysql_db.Model):
    __tablename__ = 'cart_item'
    __table_args__ = {'comment': '购物车明细表'}

    id = Column(Integer, primary_key=True, comment="购物车项ID")
    user_id = Column(Integer, ForeignKey('user.id'), comment="用户ID")
    product_id = Column(Integer, ForeignKey('product.id'), comment="商品ID")
    quantity = Column(Integer, default=1, comment="数量")
    selected = Column(Boolean, default=True, comment="是否选中")

    user = relationship("User", back_populates="cart_items")
    product = relationship("Product", back_populates="cart_items")


class Address(mysql_db.Model):
    __tablename__ = 'address'
    __table_args__ = {'comment': '用户地址表'}

    id = Column(Integer, primary_key=True, comment="地址ID")
    user_id = Column(Integer, ForeignKey('user.id'), comment="用户ID")
    contact_name = Column(String(50), comment="收货人姓名")
    contact_phone = Column(String(20), comment="联系电话")
    full_address = Column(Text, comment="完整地址")
    is_default = Column(Boolean, default=False, comment="是否默认地址")

    user = relationship("User", back_populates="addresses")


class Coupon(mysql_db.Model):
    __tablename__ = 'coupon'
    __table_args__ = {'comment': '优惠券表'}

    id = Column(Integer, primary_key=True, comment="优惠券ID")
    code = Column(String(50), unique=True, comment="优惠券编码")
    discount = Column(Numeric(10, 2), comment="优惠金额")
    valid_from = Column(DateTime, comment="有效期起")
    valid_to = Column(DateTime, comment="有效期止")
    usage_limit = Column(Integer, comment="可使用次数")

    product_coupons = relationship("ProductCoupon", back_populates="coupon")
